package com.shure.modules.docs.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.shure.common.query.condition.Condition;
import com.shure.modules.docs.entity.Docs;
import com.shure.modules.docs.entity.Folders;
import com.shure.modules.docs.query.FoldersQuery;
import com.shure.modules.docs.service.DocsService;
import com.shure.modules.docs.service.FoldersService;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/folder")
public class FolderController {

    private final FoldersService folderService;
    private final DocsService docsService;

    public FolderController(FoldersService folderService, DocsService docsService) {
        this.folderService = folderService;
        this.docsService = docsService;
    }

    // 新增文件夹
    @PostMapping
    public ResponseEntity create(@RequestBody Folders folder) {
        boolean b = folderService.save(folder);
        return ResponseEntity.ok(b);
    }

    // 查询文件夹列表
    @GetMapping("tree")
    public ResponseEntity<List<Folders>> tree(FoldersQuery query) {
        LambdaQueryWrapper<Folders> wrapper = Condition.getLambdaQueryWrapper(query);
        List<Folders> allFolders = folderService.list(wrapper);
        // 构建树形结构
        List<Folders> tree = buildTree(allFolders);
        return ResponseEntity.ok(tree);
    }

    private List<Folders> buildTree(List<Folders> folders) {
        // 按父ID分组
        Map<Integer, List<Folders>> parentIdMap = folders.stream().collect(Collectors.groupingBy(Folders::getPId));

        // 获取所有根节点(父ID为0的节点)
        List<Folders> roots = parentIdMap.getOrDefault(0, new ArrayList<>());

        // 为每个根节点设置子节点
        roots.forEach(root -> setChildren(root, parentIdMap));

        return roots;
    }

    private void setChildren(Folders parent, Map<Integer, List<Folders>> parentIdMap) {
        // 获取当前节点的子节点
        List<Folders> children = parentIdMap.get(parent.getId().intValue());

        if (children != null && !children.isEmpty()) {
            parent.setChildren(children);
            // 递归设置子节点的子节点
            children.forEach(child -> setChildren(child, parentIdMap));
        }
    }

    // 查询文件夹
    @GetMapping("/{id}")
    public ResponseEntity<Folders> getById(@PathVariable("id") Long id) {
        Folders folder = folderService.getById(id);
        return ResponseEntity.ok(folder);
    }

    // 更新文件夹
    @PutMapping("/{id}")
    public ResponseEntity update(@PathVariable("id") Long id, @RequestBody Folders folder) {
        folder.setId(id);
        return ResponseEntity.ok(folderService.updateById(folder));
    }

    // 删除文件夹
    @DeleteMapping("/{id}")
    public ResponseEntity delete(@PathVariable("id") Long id) {
        List<Folders> folders = folderService.list(new LambdaQueryWrapper<Folders>().eq(Folders::getPId, id));
        if (!folders.isEmpty()) {
            return ResponseEntity.status(400).body("存在子目录，无法删除");
        }
        List<Docs> docs = docsService.list(new LambdaQueryWrapper<Docs>().eq(Docs::getFolderId, id));
        if (!docs.isEmpty()) {
            return ResponseEntity.status(400).body("存在文档，无法删除");
        }
        folderService.removeById(id);
        return ResponseEntity.noContent().build();
    }
}
